package top.lixunda.books3.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.lixunda.books3.dao.BookMapper;
import top.lixunda.books3.dao.UserBookTagMapper;
import top.lixunda.books3.entity.Book;
import top.lixunda.books3.entity.User;
import top.lixunda.books3.entity.UserBookTag;
import top.lixunda.books3.entity.UserBookTagExample;
import top.lixunda.books3.model.BookTag;
import top.lixunda.books3.model.SimpleBook;
import top.lixunda.books3.service.RecommendService;
import top.lixunda.books3.util.Log;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RecommendServiceImpl implements RecommendService {

    public static final long PAGE_COUNT = 20;

    private final UserBookTagMapper userBookTagMapper;

    private final BookMapper bookMapper;

    @Autowired
    public RecommendServiceImpl(UserBookTagMapper userBookTagMapper, BookMapper bookMapper) {
        this.userBookTagMapper = userBookTagMapper;
        this.bookMapper = bookMapper;
    }

    /**
     * 保存用户的点击书籍信息
     * 会将书籍的tag保存到用户的行为表中
     * @param user 点击书籍的信息，可以为空，为空时不做任何操作
     * @param book 点击的书籍，必须包含tags信息
     * @return 返回插入是否成功
     */
    @Override
    @Transactional
    public void insertBookTags(User user, Book book) {
        //1. 检验参数正确性
        if(user == null || book == null){
            return;
        }
        String tags[] = book.getTags().trim().split(" ");
        for(String tag : tags){
            UserBookTagExample example = new UserBookTagExample();
            UserBookTagExample.Criteria criteria = example.createCriteria();
            criteria.andUserEqualTo(user.getSystemid());
            criteria.andTagEqualTo(tag);
            List<UserBookTag> userBookTagList = userBookTagMapper.selectByExample(example);
            //大于零表示存在，则修改内容，使times加一
            if(userBookTagList.size() > 0){
                UserBookTag userBookTag = userBookTagList.get(0);
                userBookTag.setTimes(userBookTag.getTimes()+1);
                userBookTagMapper.updateByPrimaryKeySelective(userBookTag);
                Log.d("-------------》  更改标签行为: "+tag+"  "+userBookTag.getTimes());
            }else{
                //不存在，插入新的记录
                UserBookTag bookTag = new UserBookTag();
                bookTag.setTag(tag);
                bookTag.setUser(user.getSystemid());
                bookTag.setTimes(1L);
                bookTag.setTime(new Date().getTime());
                userBookTagMapper.insert(bookTag);
                Log.d("-------------》  新插入标签行为: "+tag);
            }
        }
    }

    @Override
    @Transactional
    public Map<String, Object> querySimpleBookAndCountByUserTag(User user, long page) {
        return querySimpleBookAndCountByUserTag(user, page*PAGE_COUNT, PAGE_COUNT);
    }

    @Override
    public Map<String, Object> querySimpleBookAndCountByUserTag(User user, long start, long count) {
        Map<String, Object> map = new HashMap<>();
        List<BookTag> bookTags = queryBookTagByUser(user, 5);
        map.put("simpleBook", querySimpleBookByUserTag(bookTags, start, count));
        map.put("count", queryCountByUserTag(bookTags));
        return map;
    }

    @Override
    public List<BookTag> queryBookTagByUser(User user, long limit) {
        if(user == null){
            return userBookTagMapper.selectBookTagByLimit(limit);
        }
        List<BookTag> bookTags = userBookTagMapper.selectBookTagByUserAndLimit(user, limit);
        return bookTags.size() == 0 ? userBookTagMapper.selectBookTagByLimit(limit) : bookTags;
    }

    private List<SimpleBook> querySimpleBookByUserTag(List<BookTag> bookTags, long start, long count) {
        return bookMapper.selectSimpleBookByBookTags(bookTags, start, count);
    }

    private long queryCountByUserTag(List<BookTag> bookTags) {
        return bookMapper.getCountByBookTagList(bookTags);
    }


}
